﻿Public Class WebForm1

    '   Created by          :   Daniel Ramsdell
    '   Created on          :   15/November/2011
    '   Last Modified       :   28/November/2011
    '   Last Modification   :   Update code validation, create continue button
    '   Porpose             :   To generate data for the summery page
    '   TODO                :   Aquire user ID and Flight ID from previous page         Done
    '                           Create button to send data to the next page             Done
    '                           Testing                                                 Done
    '                           Give page more professional design
    '   Known Bugs          :   

    Inherits System.Web.UI.Page
    Dim Flight_Number As Integer
    Dim User_Id As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Flight_Number = Convert.ToInt32(Request("ctl00$MainContent$txtFlightNum"))
        'User_Id = Convert.ToInt32(Request("ctl00$MainContent$txtUserId"))
        btnContinue.PostBackUrl = "Payment.aspx"

        If txtFlightNumber.Value = "" Then
            txtFlightNumber.Value = Request("ctl00$MainContent$txtFlightNumber")

        End If

        If txtUserId.Value = "" Then
            txtUserId.Value = Request("ctl00$MainContent$txtUserId")

        End If
        If txtCost.Value = "" Then
            txtCost.Value = Request("ctl00$MainContent$txtCost")

        End If

        Flight_Number = Convert.ToInt32(txtFlightNumber.Value)
        User_Id = Convert.ToInt32(txtUserId.Value)

        GetSummery()
    End Sub

    Sub GetSummery()
        '*** DataSet From DataSet ***'
        Dim ds As DataSet

        '   Send quiry to MYSQL
        ds = CreateDataSet("SELECT " _
                            & "u.first_name, u.last_name, " _
                            & "uf.flight_id, uf.num_seat, " _
                            & "f.departure, f.arrive_time, f.cost, " _
                            & "r.destination, r.from " _
                            & "FROM " _
                            & "user u, user_flight uf, flight f, route r " _
                            & "WHERE " _
                            & "u.id = " & User_Id & " AND " & Flight_Number & " = f.id AND u.id = uf.user_id AND uf.flight_id = f.id AND f.route_id = r.id")

        '   Send data to page
        Flight.Text = "Flight number:"
        dtaFlight.Text = ds.Tables(0).Rows(0)(2).ToString()

        Name.Text = "Name:"
        dtaName.Text = ds.Tables(0).Rows(0)(0).ToString() + " " + ds.Tables(0).Rows(0)(1).ToString()

        Departure.Text = "Departure:"
        dtaDeparture.Text = ds.Tables(0).Rows(0)(8).ToString()

        Destination.Text = "Destination:"
        dtaDestination.Text = ds.Tables(0).Rows(0)(7).ToString()

        Seat.Text = "Seat Number:"
        dtaSeat.Text = ds.Tables(0).Rows(0)(3).ToString()

        DepartTime.Text = "Departure time:"
        dtaDepartTime.Text = ds.Tables(0).Rows(0)(4).ToString()

        ArriveTime.Text = "Landing Time:"
        dtaArriveTime.Text = ds.Tables(0).Rows(0)(5).ToString()

        Cost.Text = "Price:"
        dtaCost.Text = ds.Tables(0).Rows(0)(6).ToString()
        txtCost.Value = ds.Tables(0).Rows(0)(6).ToString()


    End Sub

    '*** Gets a DataSet from the routes table ***'
    Function CreateDataSet(srtSql As String) As DataSet
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand
        Dim dtAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter

        Dim ds As New DataSet
        Dim strConnString

        strConnString = "Server=localhost;User Id=goldair_dapp; Password=geckodev; Database=goldair_db; Pooling=false"


        objConn.ConnectionString = strConnString
        With objCmd
            .Connection = objConn
            .CommandText = srtSql
            .CommandType = CommandType.Text
        End With
        dtAdapter.SelectCommand = objCmd

        Try
            dtAdapter.Fill(ds, "route")
        Catch ex As Exception

        End Try


        dtAdapter = Nothing
        objConn.Close()
        objConn = Nothing

        Return ds   '*** Return DataSet ***'

    End Function


End Class